package cn.smxy.ln.controller;

import cn.smxy.ln.common.lang.Result;
import cn.smxy.ln.common.utils.RedisUtil;
import cn.smxy.ln.pojo.College;
import cn.smxy.ln.pojo.Department;
import cn.smxy.ln.pojo.User;
import cn.smxy.ln.service.CollegeService;
import cn.smxy.ln.service.DepartmentService;
import cn.smxy.ln.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ln
 * @date 2022年05月24日 19:14
 */
@RestController
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    CollegeService collegeService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    UserService userService;

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/list")
    @Cacheable(cacheNames = "college", unless = "#result.getData() == null")
    //key:缓存数据时使用的 key，可以用它来指定。默认是使用方法参数的值；cacheNames/value：二者选其一即可，指定缓存组件的名字
    @HystrixCommand(fallbackMethod = "hystrixGetList")
    public Result queryAll() {
        List<College> list = collegeService.list();
        if (list == null) {
            throw new RuntimeException("数据库没有college数据，或者信息无法找到");
        }
        return Result.succ(list);
    }

    public Result hystrixGetList() {
        return Result.fail("没有得到对应的信息，数据库college是空的，或者刷新试试");
    }

    @PostMapping("/save")
    @Transactional
    public Result save(@RequestBody College college) {
        if ((college.getName() == null || college.getName().equals("")) && (college.getMemo() == null || college.getMemo().equals("")))
            return Result.fail("college为空，保存失败");
        if (college.getName() == null || college.getName().equals(""))
            return Result.fail("name不能为空，保存失败");
        boolean flag = collegeService.save(college);
        if (flag) {
            redisUtil.flushByX("college");
            return Result.succ(college);
        }
        return Result.fail("保存失败");
    }

    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestParam Long id) {
        List<Department> departments = departmentService.list();
        List<User> users = userService.list();
        for (Department d: departments) {
            if (d.getCollegeId() == id) {
                d.setCollegeId(Long.valueOf(0));
                departmentService.updateById(d);
            }
        }
        redisUtil.flushByX("department");
        for (User u: users) {
            if (u.getCollegeId() == id) {
                u.setCollegeId(Long.valueOf(0));
                userService.updateById(u);
            }
        }
        boolean flag = collegeService.removeById(id);
        if (flag) {
            redisUtil.flushByX("college");
            return Result.succ("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    @Transactional
    public Result update(@RequestBody College college) {
        if ((college.getName() == null || college.getName().equals("")) && (college.getMemo() == null || college.getMemo().equals("")))
            return Result.fail("college为空，更新失败");
        if (college.getName() == null || college.getName().equals(""))
            return Result.fail("name不能为空，更新失败");
        boolean flag = collegeService.updateById(college);
        if (flag) {
            redisUtil.flushByX("college");
            return Result.succ(college);
        }
        return Result.fail("修改失败");
    }

}
